package cat.joronya.supermanager.market;

import cat.joronya.supermanager.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class SortDialogFragment extends DialogFragment
{
	private OnSelectedSortCriteriaListener mSortListener;
	
	public interface OnSelectedSortCriteriaListener {
		public void selectSortCriteriaOrder(int item);
	}
	
	public static SortDialogFragment newInstance(int checked)
	{
		SortDialogFragment frag = new SortDialogFragment();
		Bundle args = new Bundle();
        args.putInt("cat.joronya.supermanager.martket.sort.checked", checked);
        frag.setArguments(args);
		return frag;
	}
	
	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
        	mSortListener = (OnSelectedSortCriteriaListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnSelectedSortCriteriaListener");
        }
    }
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) 
	{
		final int checked = getArguments().getInt("cat.joronya.supermanager.martket.sort.checked");
		
		final CharSequence[] items = {
				getString(R.string.market_order_by_price),
				getString(R.string.market_order_by_average),
				getString(R.string.market_order_by_rating),
				getString(R.string.market_order_by_rating_last3_games),
				getString(R.string.market_order_by_increase_15),
				getString(R.string.market_order_by_keep_price),
				getString(R.string.market_order_by_decrease_15)};
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(getString(R.string.market_order_title));
		builder.setSingleChoiceItems(items, checked, new DialogInterface.OnClickListener() {
		    public void onClick(final DialogInterface dialog, int item) 
		    {
		    	mSortListener.selectSortCriteriaOrder(item);
		    }
		});
		
		return builder.create();
	}
}
